﻿using IRepositoryBll;
using Microsoft.AspNetCore.Mvc;
using RepositoryEntity;
using RepositorySystem.Models;
using RepositorySystem.Models.UserInfoViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RepositorySystem.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    public class UserInfoController : Controller
    {
        private IUserInfoBll _userInfoBll;

        public UserInfoController(IUserInfoBll userInfoBll)
        {
            _userInfoBll = userInfoBll;
        }

        public IActionResult UserInfoListView(AddUserInfoViewModel model)
        {
            return View();
        }

        /// <summary>
        /// 添加用户用页面
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IActionResult AddUserInfoView(AddUserInfoViewModel model)
        {
            return View();
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddUserInfo(AddUserInfoViewModel model)
        {
            CustomActionResult res = new CustomActionResult();

            if (string.IsNullOrEmpty(model.Account))
            {
                res.Msg = "账号不能为空";
                return Json(res);
            }
            else if (string.IsNullOrEmpty(model.PassWord))
            {
                res.Msg = "密码不能为空";
                return Json(res);
            }
            else if (string.IsNullOrEmpty(model.UserName))
            {
                res.Msg = "用户名不能为空";
                return Json(res);
            }

            UserInfo entity = new UserInfo()
            {
                ID = Guid.NewGuid().ToString(),
                Account = model.Account,
                UserName = model.UserName,
                PhoneNum = model.PhoneNum,
                Email = model.Email,
                Sex = model.Sex,
                PassWord = model.PassWord,
                CreateTime = DateTime.Now,
                IsDelete = false
            };

            bool isSuccess = _userInfoBll.AddUserInfo(entity);
            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Msg = "添加成功";
                return Json(res);
            }
            else
            {
                return Json(res);
            }
        }

        /// <summary>
        /// 修改用户用页面
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IActionResult UpdateUserInfoView(string userInfoId)
        {
            if (!string.IsNullOrEmpty(userInfoId))
            {
                UserInfo userInfo = _userInfoBll.GetUserInfo(userInfoId);
                if (userInfo != null)
                {
                    ViewBag.ID = userInfo.ID;
                    ViewBag.UserName = userInfo.UserName;
                    ViewBag.Account = userInfo.Account;
                    ViewBag.PassWord = userInfo.PassWord;
                    ViewBag.PhoneNum = userInfo.PhoneNum;
                    ViewBag.Email = userInfo.Email;
                    ViewBag.Sex = userInfo.Sex;
                }
            }

            return View();
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IActionResult UpdateUserInfo(UpdateUserInfoViewModel model)
        {

            if (_userInfoBll.UpdateUserInfo(model.ID, model.UserName, model.Sex))
            {
                return Json("OK");
            }
            else
            {
                return Json("Error");
            }
        }




        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteUserInfo(string userInfoId)
        {
            if (_userInfoBll.SoftDeleteUserInfo(userInfoId))
            {
                return Json("OK");
            }
            else
            {
                return Json("Error");
            }
        }

        /// <summary>
        /// 根据id获取用户信息
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <returns></returns>
        public IActionResult GetUserInfo(string userInfoId)
        {
            UserInfo userInfo = _userInfoBll.GetUserInfo(userInfoId);
            if (userInfo == null)
            {
                return Json("Error");
            }
            else
            {
                return Json(userInfo);
            }
        }

        /// <summary>
        /// /获取用户集合
        /// </summary>
        /// <returns></returns>
        public IActionResult GetUserInfoList()
        {
            List<UserInfo> userInfos = _userInfoBll.GetUserInfoList();

            List<object> list = new List<object>();
             
            foreach (var item in userInfos)
            {
                string sexStr;
                if (item.Sex == 1)
                {
                    sexStr = "男";
                }
                else
                {
                    sexStr = "女";
                }

                list.Add(new
                {
                    item.UserName,
                    item.ID,
                    item.Account,
                    item.PhoneNum,
                    item.Email,
                    CreateTime = item.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                    Sex = sexStr
                }); 
            }

            return Json(new 
            {
                code = 0,
                msg = "成功",
                count = userInfos.Count,
                data = list
            });
        }
    }
}
